//import 'package:amap_base/amap_base.dart';

import 'dart:convert';

import 'package:date_format/date_format.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:flutter_hotwork/bean/AllRiBaoBiaoBean.dart';
import 'package:flutter_hotwork/home/RoadChoice.dart';
import 'package:flutter_hotwork/utils/DioUtils.dart';

import '../LoadingDialog.dart';
import '../Myapplication.dart';
import '../Toast.dart';

class tongjibaobiaoallzongbiaoPage extends StatefulWidget {
  @override
  tongjibaobiaorihuizongbiaoState createState() => new tongjibaobiaorihuizongbiaoState();
}

class tongjibaobiaorihuizongbiaoState extends State<tongjibaobiaoallzongbiaoPage> {

  Map selectArea;
  bool hasdata=false;
  allribaobiaobean allhuizongbean;

  double sd1=0.0;
  double sd2=0.0;
  double sd3=0.0;
  double sm1=0.0;
  double sm2=0.0;
  double sm3=0.0;
  double sy1=0.0;
  double sy2=0.0;
  double sy3=0.0;
  double sdgs=0.0;
  double smgs=0.0;
  double sygs=0.0;





  ///接收选择的结果
  void handleSelect (Map targetArea) {
    setState(() {
      selectArea = targetArea;
      getribaobiaoList();
      // initData(selectArea['proviceIndex'],selectArea['cityIndex']);
    });
  }
  @override
  void initState() {
    super.initState();
    DioUtils.createInstance();
  }

  @override
  void dispose() {
//    _amapLocation.stopLocate();
    super.dispose();
  }
  var time = formatDate(new DateTime.now(),[yyyy,'-',mm,"-",dd]);
  var time2 = formatDate(new DateTime.now(),[yyyy,'-',mm]);
  var time3 = formatDate(new DateTime.now(),[yyyy]);
  void getribaobiaoList() async {
    showDialog(
        context: context,
        builder: (context) {
          return new LoadingDialog();
        });
    try {
      Response response;


      if(MyApplication.loginuserinfo.userInfo.amHotuser==0){
        response = await DioUtils.dio.get(
            DioUtils.API_PREFIX + "/statistics/report/getcwallday?ym="+time+""+"&meterid="+(selectArea != null ? selectArea['id'] : ""));//selectArea['id']
      }else{
        response = await DioUtils.dio.get(
            DioUtils.API_PREFIX + "/statistics/report/getcwallday?ym="+time+""+"&meterid="+(selectArea != null ? selectArea['id'] :  MyApplication.loginuserinfo.userInfo.hotuserId));//selectArea['id']
      }


      Map respMap = json.decode(response.data);
      allhuizongbean = allribaobiaobean.fromJson(respMap);
      Navigator.pop(context);
      if (allhuizongbean.code == 0) {
        hasdata=true;
        sd1=0.0;
        sd2=0.0;
        sd3=0.0;
        sm1=0.0;
        sm2=0.0;
        sm3=0.0;
        sy1=0.0;
        sy2=0.0;
        sy3=0.0;
        sdgs=0.0;
        smgs=0.0;
        sygs=0.0;
        for(int i = 0;i<allhuizongbean.result.length;i++){
          sd1+=allhuizongbean.result[i].sd1;
          sd2+=allhuizongbean.result[i].sd2;
          sd3+=allhuizongbean.result[i].sd3;
          sm1+=allhuizongbean.result[i].sm1;
          sm2+=allhuizongbean.result[i].sm2;
          sm3+=allhuizongbean.result[i].sm3;
          sy1+=allhuizongbean.result[i].sy1;
          sy2+=allhuizongbean.result[i].sy2;
          sy3+=allhuizongbean.result[i].sy3;
          // sdgs+=allhuizongbean.result[i].sdgs;
          // smgs+=allhuizongbean.result[i].smgs;
          // sygs+=allhuizongbean.result[i].sygs;
        }
        sdgs = ((sd1 - sd2) / sd1)*100;
        smgs = ((sm1 - sm2) / sm1)*100;
        sygs = ((sy1 - sy2) / sy1)*100;
        setState(() {

        });
      } else {
         LocalToast.toast(context,msg: allhuizongbean.message);
      }
    } catch (e) {
      Navigator.pop(context);
       LocalToast.toast(context,msg: "网络错误");
    }
  }

  @override
  Widget build(BuildContext context) {
    ScrollController controller = ScrollController();
    double rpx = MediaQuery.of(context).size.width / 750;
    return Scaffold(
        body:
        new CustomScrollView(
            shrinkWrap: true,
            // 内容
            slivers: <Widget>[
              new SliverPadding(
                  padding: const EdgeInsets.all(0),
                  sliver: new SliverList(
                      delegate: new SliverChildListDelegate(
                        <Widget>[
                          Container(
                            child: Row(
                              children: [
                                Container(
                                  margin:  EdgeInsets.only(left: 0, right: 0, top:5, bottom: 5),
                                  child:
                                  InkWell(
                                    onTap: (){
                                      showBottomSheet(context: context, builder: (_)=>Container(
                                        height: 200.0,
                                        child: AreaSelection(
                                          onSelect: handleSelect,
                                          initProviceIndex: selectArea == null ? 0 : selectArea['proviceIndex'],
                                          initCityIndex: selectArea == null ? 0 : selectArea['cityIndex'],
                                          // initCountyIndex: selectArea == null ? 0 : selectArea['countyIndex'],
                                        ),
                                      ));
                                    },
                                    child:
                                    Container(
                                        height: 20,
                                        margin: EdgeInsets.only(left: 10, right: 0, top:0, bottom: 0),
                                        padding:  EdgeInsets.only(left: 2, right: 0, top:0, bottom: 0),
                                        decoration: BoxDecoration(
                                          border: new Border.all(color: Colors.blue, width: 0.5), // 边色与边宽度
                                          color: Colors.white, // 底色
                                          //        borderRadius: new BorderRadius.circular((20.0)), // 圆角度
                                        ),
                                        child:
                                        Row(
                                          children: [
                                            Container(
                                              width:100,
                                              child:
                                              Text(selectArea != null ? selectArea['address'] : "请选择用户"  ,
                                                style: TextStyle(color: Colors.black,fontSize: 10),
                                                overflow: TextOverflow.ellipsis,
                                              ) ,
                                            ),

                                            Container(width: 5,),
                                            Image.asset(
                                              'images/jiantou.png',
                                              width: 14.0,
                                              height: 20.0,
                                            ),
                                          ],
                                        )
                                    ),



                                  ),
                                ),

                                FlatButton(
                                  onPressed: () {
                                    DatePicker.showDatePicker(context,
                                        // 是否展示顶部操作按钮
                                        showTitleActions: true,
                                        // 最小时间
                                        minTime: DateTime(2018, 3, 5),
                                        // 最大时间
                                        maxTime: DateTime(2099, 6, 7),
                                        // change事件
                                        onChanged: (date) {

                                          print('change $date');
                                        },
                                        // 确定事件
                                        onConfirm: (date) {
                                          time = formatDate(date,[yyyy,'-',mm,"-",dd]);
                                          time2 = formatDate(date,[yyyy,'-',mm]);
                                          time3 = formatDate(date,[yyyy]);
                                          setState(() {
                                            getribaobiaoList();
                                          });
                                          print('confirm $time');
                                        },
                                        // 当前时间
                                        currentTime: DateTime.now(),
                                        // 语言
                                        locale: LocaleType.zh);
                                  },
                                  child:
                                  Container(
                                      width: 100,
                                      height: 20,
                                      margin: EdgeInsets.only(left: 10, right: 0, top:0, bottom: 0),
                                      padding:  EdgeInsets.only(left: 10, right: 0, top:0, bottom: 0),
                                      decoration: BoxDecoration(
                                        border: new Border.all(color: Colors.blue, width: 0.5), // 边色与边宽度
                                        color: Colors.white, // 底色
                                        //        borderRadius: new BorderRadius.circular((20.0)), // 圆角度
                                      ),
                                      child:
                                      Row(
                                        children: [
                                          Image.asset(
                                            'images/rili.png',
                                            width: 14.0,
                                            height: 20.0,
                                          ),
                                          Container(width: 5,),
                                          Text(
                                            '$time',
                                            style: TextStyle(color: Colors.black,fontSize: 10),
                                          )

                                        ],
                                      )
                                  ),
                                ),

                                InkWell(
                                  onTap: (){
                                    getribaobiaoList();
                                  },
                                  child:
                                  Container(
                                      width: 50,
                                      height: 20,
                                      margin: EdgeInsets.only(left: 10, right: 0, top:0, bottom: 0),
                                      padding:  EdgeInsets.only(left: 2, right: 2, top:0, bottom: 0),
                                      decoration: BoxDecoration(
                                        // border: new Border.all(color: Colors.blue, width: 0.5), // 边色与边宽度
                                        color: Colors.blue, // 底色
                                        borderRadius: new BorderRadius.circular((5.0)), // 圆角度
                                      ),
                                      child:
                                      Row(
                                        mainAxisAlignment:MainAxisAlignment.center,
                                        children: [
                                          Text("查询",
                                            style: TextStyle(color: Colors.white,fontSize: 10),
                                          )

                                        ],
                                      )
                                  ),
                                ),
                              ],
                            ),
                          ),

                          Row(

                            children: [
                              Flexible(fit: FlexFit.tight, child: SizedBox()),
                              Text("供热日报表",
                                style: TextStyle(color: Colors.black87,fontSize: 12,fontWeight: FontWeight.bold),
                              ),
                              Flexible(fit: FlexFit.tight, child: SizedBox()),
                            ],
                          ),


                          //根据是否有数据展示
                          hasdata?
                          Container(
                            child:
                            Column(
                                children: [

                                  Container(
                                    width: rpx*730,
                                    height: 0.5,
                                    color: Colors.grey,
                                  ),
                                  Container(
                                    width: rpx*730,
                                    height: 5,
                                  ),


                                  Container(
                                    width: rpx*750,
                                    child:
                                    // Expanded(
                                    //   child:
                                    genContentList(
                                        allhuizongbean.result,context, controller
                                    ),
                                    // ),
                                  ),
                                  Container(
                                    padding: EdgeInsets.only(left: 17,right: 0,top: 0,bottom: 0),
                                    child:

                                  Column(
                                    children: [
                                      Row(
                                        crossAxisAlignment:CrossAxisAlignment.center,
                                        children: [
                                          Text("合计",style: TextStyle(fontSize: 10),),
                                          Container(width: 10,),
                                        ],),
                                      Row(
                                        crossAxisAlignment:CrossAxisAlignment.center,
                                        children: [
                                          Text("日",style: TextStyle(fontSize: 10),),
                                          Container(width: 10,),
                                          Text("供汽量:"+sd1.toString().substring(0,sd1.toString().lastIndexOf(".")+2).toString()
                                            ,style: TextStyle(fontSize: 10),),
                                          Container(width: 10,),
                                          Text("售汽量:"+sd2.toString().substring(0,sd2.toString().lastIndexOf(".")+2).toString()
                                            ,style: TextStyle(fontSize: 10),),
                                          Container(width: 10,),
                                          Text("管损(t):"+sd3.toString().substring(0,sd3.toString().lastIndexOf(".")+2).toString()
                                            ,style: TextStyle(fontSize: 10),),
                                        ],),
                                      Row(
                                        crossAxisAlignment:CrossAxisAlignment.center,
                                        children: [
                                          Text("月",style: TextStyle(fontSize: 10),),
                                          Container(width: 10,),
                                          Text("供汽量:"+sm1.toString().substring(0,sm1.toString().lastIndexOf(".")+2).toString(),style: TextStyle(fontSize: 10),),
                                          Container(width: 10,),
                                          Text("售汽量:"+sm2.toString().substring(0,sm2.toString().lastIndexOf(".")+2).toString(),style: TextStyle(fontSize: 10),),
                                          Container(width: 10,),
                                          Text("管损(t):"+sm3.toString().substring(0,sm3.toString().lastIndexOf(".")+2).toString(),style: TextStyle(fontSize: 10),),
                                        ],),
                                      Row(
                                        crossAxisAlignment:CrossAxisAlignment.center,
                                        children: [
                                          Text("年",style: TextStyle(fontSize: 10),),
                                          Container(width: 10,),
                                          Text("供汽量:"+sy1.toString().substring(0,sy1.toString().lastIndexOf(".")+2).toString(),style: TextStyle(fontSize: 10),),
                                          Container(width: 10,),
                                          Text("售汽量:"+sy2.toString().substring(0,sy2.toString().lastIndexOf(".")+2).toString(),style: TextStyle(fontSize: 10),),
                                          Container(width: 10,),
                                          Text("管损(t):"+sy3.toString().substring(0,sy3.toString().lastIndexOf(".")+2).toString(),style: TextStyle(fontSize: 10),),
                                        ],),
                                      Row(
                                        crossAxisAlignment:CrossAxisAlignment.center,
                                        children: [
                                          Text("管损率",style: TextStyle(fontSize: 10),),
                                          Container(width: 10,),
                                          Text("日:"+sdgs.toString().substring(0,sdgs.toString().lastIndexOf(".")+3)+"%",style: TextStyle(fontSize: 10),),
                                          Container(width: 10,),
                                          Text("月:"+smgs.toString().substring(0,smgs.toString().lastIndexOf(".")+3)+"%",style: TextStyle(fontSize: 10),),
                                          Container(width: 10,),
                                          Text("年:"+sygs.toString().substring(0,sygs.toString().lastIndexOf(".")+3)+"%",style: TextStyle(fontSize: 10),),
                                        ],),
                                    ],
                                  ),
                                  ),


                                ]),
                          ) //根据是否有数据展示
                              :Container()
                        ],
                      )))])





    );

  }


  genContentList(List<Result> data, BuildContext context, controller) {
    double rpx = MediaQuery
        .of(context)
        .size
        .width / 750;

    return ListView.builder(
      shrinkWrap: true,
      controller: controller,
      itemCount: data.length,
      itemBuilder: (BuildContext context, int index) {
        return
          ExpansionTile(
              title:
              Column(
                children: [
                  Row(
                    crossAxisAlignment:CrossAxisAlignment.center,
                    children: [
                      Text(data[index].lineName,style: TextStyle(fontSize: 10),),
                      Container(width: 10,),
                    ],),
                  Row(
                    crossAxisAlignment:CrossAxisAlignment.center,
                    children: [
                      Text("日",style: TextStyle(fontSize: 10),),
                      Container(width: 10,),
                      Text("供汽量:"+data[index].sd1.toString()
                        ,style: TextStyle(fontSize: 10),),
                      Container(width: 10,),
                      Text("售汽量:"+data[index].sd2.toString(),style: TextStyle(fontSize: 10),),
                      Container(width: 10,),
                      Text("管损(t):"+data[index].sd3.toString(),style: TextStyle(fontSize: 10),),
                    ],),
                  Row(
                    crossAxisAlignment:CrossAxisAlignment.center,
                    children: [
                      Text("月",style: TextStyle(fontSize: 10),),
                      Container(width: 10,),
                      Text("供汽量:"+data[index].sm1.toString(),style: TextStyle(fontSize: 10),),
                      Container(width: 10,),
                      Text("售汽量:"+data[index].sm2.toString(),style: TextStyle(fontSize: 10),),
                      Container(width: 10,),
                      Text("管损(t):"+data[index].sm3.toString(),style: TextStyle(fontSize: 10),),
                    ],),
                  Row(
                    crossAxisAlignment:CrossAxisAlignment.center,
                    children: [
                      Text("年",style: TextStyle(fontSize: 10),),
                      Container(width: 10,),
                      Text("供汽量:"+data[index].sy1.toString(),style: TextStyle(fontSize: 10),),
                      Container(width: 10,),
                      Text("售汽量:"+data[index].sy2.toString(),style: TextStyle(fontSize: 10),),
                      Container(width: 10,),
                      Text("管损(t):"+data[index].sy3.toString(),style: TextStyle(fontSize: 10),),
                    ],),
                  Row(
                    crossAxisAlignment:CrossAxisAlignment.center,
                    children: [
                      Text("管损率",style: TextStyle(fontSize: 10),),
                      Container(width: 10,),
                      Text("日:"+data[index].sdgs.toString()+"%",style: TextStyle(fontSize: 10),),
                      Container(width: 10,),
                      Text("月:"+data[index].smgs.toString()+"%",style: TextStyle(fontSize: 10),),
                      Container(width: 10,),
                      Text("年:"+data[index].sygs.toString()+"%",style: TextStyle(fontSize: 10),),
                      Container(width: 10,),
                      Text("备注:"+data[index].remark.toString(),style: TextStyle(fontSize: 10),),
                    ],),
                ],
              ),


              children:

              data[index].lists.map((posi) =>
                  Container(
                    padding: EdgeInsets.only(left: 20, right: 10, top: 0, bottom: 0),
                    child:
                    Column(
                      children: [
                        Row(
                          crossAxisAlignment:CrossAxisAlignment.center,
                          children: [
                            Text("用户编号:"+posi.userNumber,style: TextStyle(fontSize: 10),),
                            Container(width: 10,),
                            Text("用户名称:"+posi.company,style: TextStyle(fontSize: 10),),
                            Container(width: 10,),
                          ],),
                        Row(
                          crossAxisAlignment:CrossAxisAlignment.center,
                          children: [
                            Text("前日底数(t):"+posi.lastAccumulation.toString(),style: TextStyle(fontSize: 10),),
                            Container(width: 10,),
                            Text("本日底数(t):"+posi.accumulation.toString()
                              ,style: TextStyle(fontSize: 10),),
                            Container(width: 10,),

                          ],),
                        Row(
                          crossAxisAlignment:CrossAxisAlignment.center,
                          children: [
                            Text("前日用量(t):"+posi.beforeSettlementAmount.toString(),style: TextStyle(fontSize: 10),),
                            Container(width: 10,),
                            Text("本日用量(t):"+posi.settlementAmount.toString(),style: TextStyle(fontSize: 10),),
                            Container(width: 10,),
                            Text("变化率(%):"+posi.rateChange.toString()+"%",style: TextStyle(fontSize: 10),),
                            Container(width: 10,),
                            Text("备注:"+posi.remark.toString(),style: TextStyle(fontSize: 10),),
                            Container(width: 10,),
                          ],),
                      ],
                    ),

                  )).toList()

            // Expanded(
            //   child:
            // Flexible(
            //   child:
            // getItemContentList(data[index].focusHotUserVos,context,controller),
            // )
            // )

          );



        // Expanded(
        //   child:
        // Flexible(
        //   child:
        // getItemContentList(data[index].focusHotUserVos,context,controller),
        // )
        // )

      },
    );
  }



}






















